/*
 * XDR (non-XHR) extension functions for IE8+
 * Based in CORS plugin (http://plugins.jquery.com/project/cors)
 * Modified by A.Lepe (www.alepe.com, Aug 2010)
 * 
 * It supports calls using $.get and $.post only.
 * The main difference between the CORS plugin and this one are:
 *
 * 1) This method tries first to use XDR and if not available 
 * will try to use XHR. This is to prevent any alert or 
 * security message from IE.
 *
 * 2) To minimize size and target only IE8+ versions, this method
 * does not provides an alternative fall-back.
 * CORS version uses proxy_xmlhttp.js as fall-back option (see link #1 below).
 *  
 * If you want to support "OLD" browsers, an alternative fall-back 
 * can be easily implemented (instead the error alert).
 * For example, something like:
 *
 * ...
 * } catch(e) {
 *    data["proxy_url"] = url;
 *    $._get(proxy, data, callback, type);
 * }
 * ...
 *
 * in which "proxy" must be a URL where your proxy is located. 
 * Your proxy may look like:
 *
 * <?php
     //GET method example:
     $URL = $_GET["proxy_url"];
     unset($_GET["proxy_url"]);
     $params = http_build_query($_GET);
     echo file_get_contents($URL."?".$params));
 * ?>
 *
 * For POST method you may check link #2.
 *
 * NOTES:
 *
 * $.post() method it might not work as expected. XDR does
 * not send the data to the server in the same way XHR do
 * (am I missing something?). In order to have access to that
 * POST data you will need to:
 *
 * a) set:
 *      always_populate_raw_post_data = On
 *      register_long_arrays = On
 *
 * OR :
 *
 * b) import it manually (see link #3): 
    //-------- Import XDR POST data ---------
    if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
        $data = explode('&', $GLOBALS["HTTP_RAW_POST_DATA"]);
        foreach ($data as $val) {
            if (!empty($val)) {
                list($key, $value) = explode('=', $val);
                $_POST[$key] = urldecode($value);
            }
        }
    }
 *
 * Remember to add the respective headers in the server that will
 * allow the XDR calls:
      header('Access-Control-Allow-Origin: *'); //Or http://example.com
      header('Access-Control-Max-Age: 3628800');
      header('Access-Control-Allow-Methods: GET, POST');
 */
(function($) {
$._get = $.get;
$._post = $.post;
$.get = function (url, data, callback, type) {
    // try XDR
    if (jQuery.browser.msie && window.XDomainRequest) {
        var params = '';
        for (var key in data) {
            params += ((params || url.indexOf("?") != -1)?'&':'?')+key+'='+data[key];
        }
        // Use Microsoft XDR
        var xdr = new XDomainRequest();
        xdr.open("GET", url+params);
        xdr.onload = function() {
            callback(this.responseText, 'success');
        };
        xdr.send();
    } else {
        try {
            // Try using jQuery to get data
            $._get(url, data, callback, type);
        } catch(e) {
            alert(e.message);
        }
    }
};
$.post = function (url, data, callback, type) {
    // Try XDR
    if (jQuery.browser.msie && window.XDomainRequest) {
        var params = '';
        for (var key in data) {
            params += (params?'&':'')+key+'='+data[key];
        }
        // Use XDR
        var xdr = new XDomainRequest();
        xdr.open("POST", url);
        xdr.send(params);
        xdr.onload = function() {
            callback(xdr.responseText, 'success');
        };
    } else {
        try {
            // Try using jQuery to POST
            $._post(url, data, callback, type);
        } catch(e) {
            alert(e.message);
        }
    }
};
})(jQuery);
